package com.bawei.mall.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bawei.common.core.utils.JwtUtils;
import com.bawei.common.security.utils.SecurityUtils;
import com.bawei.mall.user.domain.CusUser;
import com.bawei.mall.user.domain.MallCusShop;
import com.bawei.mall.user.domain.MallProductCar;
import com.bawei.mall.user.mapper.MallCusShopMapper;
import com.bawei.mall.user.service.ICusShopService;
import com.bawei.system.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CusShopServiceImpl extends ServiceImpl<MallCusShopMapper,MallCusShop> implements ICusShopService{
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private MallCusShopMapper mallCusShopMapper;

    private static final String ACCESS_TOKEN="login_tokens:";

    @Override
    public void add(MallCusShop mallCusShop) {
        String token = SecurityUtils.getToken();
        String key = JwtUtils.getUserKey(token);
        String tokenKey = getTokenKey(key);
        String s = redisTemplate.opsForValue().get(tokenKey);
        LoginUser loginUser = JSON.parseObject(s, LoginUser.class);
        CusUser cusUser = loginUser.getCusUser();
        if(cusUser==null){
            throw new RuntimeException("用户信息已过期，请重新登录");
        }
        Integer userId = cusUser.getUserId();
        mallCusShop.setCusUserId(userId);
        mallCusShopMapper.insert(mallCusShop);
    }

    @Override
    public List<MallProductCar> findList() {
        String token = SecurityUtils.getToken();
        String key = JwtUtils.getUserKey(token);
        String userKey = getTokenKey(key);
        String cusUserJSONString = redisTemplate.opsForValue().get(userKey);
        LoginUser loginUser= JSON.parseObject(cusUserJSONString, LoginUser.class);
        CusUser cusUser = loginUser.getCusUser();
        Integer userId = cusUser.getUserId();

        List<MallProductCar> list = mallCusShopMapper.findList(userId);
        for (MallProductCar mallProductCar : list) {
            String sku1 = mallProductCar.getProductAttr();
            List<Map> list1 = new ArrayList<>();
            String[] split = sku1.split(",");
            for (int i = 0; i <split.length; i++) {
                Map map = new HashMap<>();
                map.put("key",i+1);
                map.put("value",split[i]);
                list1.add(map);
            }
            mallProductCar.setProductAttr(JSON.toJSONString(list1));
        }

        return list;
    }

    @Override
    public void delectClear() {
        String token = SecurityUtils.getToken();
        String key = JwtUtils.getUserKey(token);
        String userKey = getTokenKey(key);
        String cusUserJSONString = redisTemplate.opsForValue().get(userKey);
        LoginUser loginUser=JSON.parseObject(cusUserJSONString, LoginUser.class);
        CusUser cusUser = loginUser.getCusUser();
        Integer userId = cusUser.getUserId();
        mallCusShopMapper.delectClear(userId);
    }

    @Override
    public void quantity(Integer id, Integer quantity) {
        mallCusShopMapper.quantity(id,quantity);
    }

    @Override
    public void deleteIds(Integer ids) {
        mallCusShopMapper.deleteIds(ids);
    }

    private String getTokenKey(String key) {
        return ACCESS_TOKEN + key;
    }
}
